﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ComicsEditor
{
    public partial class FrameControl : UserControl
    {
        private PageEditor pageEditor;
        private PageInfo pageInfo;
        private Frame frame;

        public FrameControl()
        {
            InitializeComponent();
        }

        public void Activate(PageEditor pageEditor)
        {
            this.pageEditor = pageEditor;
            Visible = true;
            toolStripButtonFrameSelect.Checked = true;
            updateCurrentFrame();
            foreach (Frame f in pageInfo.Frames)
                f.Show();
            pageEditor.UpdatePage();
        }

        public void Disactivate()
        {
            resetToolStripFrame(null);
            Visible = false;
            foreach (Frame f in pageInfo.Frames)
                f.Hide();
            if (frame != null)
                frame.EndEdit();
            pageEditor.UpdatePage();
        }

        public void Bind(PageInfo pageInfo)
        {
            if (Visible && this.pageInfo != null)
                foreach (Frame f in this.pageInfo.Frames)
                    f.Hide();
            if (frame != null)
                frame.EndEdit();
            frame = null;
            updateCurrentFrame();

            this.pageInfo = pageInfo;

            listBoxFrames.Items.Clear();
            for (int i = 0; i < pageInfo.Frames.Count; i++)
                listBoxFrames.Items.Add((i + 1).ToString());
            pageInfo.FrameCreate = OnFrameCreate;
            pageInfo.FrameSelect = OnFrameSelect;

            if (Visible)
                foreach (Frame f in pageInfo.Frames)
                    f.Show();
        }

        public void OnFrameCreate(Frame frame)
        {
            listBoxFrames.Items.Add(pageInfo.Frames.Count.ToString());
            listBoxFrames.SelectedIndex = pageInfo.Frames.Count - 1;
        }

        public void OnFrameSelect(Frame frame)
        {
            listBoxFrames.SelectedIndex = pageInfo.Frames.IndexOf(frame);
        }

        private void updateCurrentFrame()
        {
            if (pageEditor == null)
                return;
            if (frame != null)
            {
                frame.BeginEdit();
                pageEditor.UpdatePage();
            }
        }

        private void resetToolStripFrame(ToolStripButton sender)
        {
            if (sender != toolStripButtonFrameSelect)
                toolStripButtonFrameSelect.Checked = false;
            if (sender != toolStripButtonFrameDraw)
                toolStripButtonFrameDraw.Checked = false;
            if (sender != toolStripButtonFrameOrder)
                toolStripButtonFrameOrder.Checked = false;
        }

        private void toolStripButtonFrameSelect_CheckedChanged(object sender, EventArgs e)
        {
            if (toolStripButtonFrameSelect.Checked)
            {
                resetToolStripFrame(toolStripButtonFrameSelect);
                pageEditor.FrameSelect.Enable();
            }
            else
                pageEditor.FrameSelect.Disable();
        }

        private void toolStripButtonFrameDraw_CheckedChanged(object sender, EventArgs e)
        {
            if (toolStripButtonFrameDraw.Checked)
            {
                resetToolStripFrame(toolStripButtonFrameDraw);
                pageEditor.FrameDraw.Enable();
            }
            else
                pageEditor.FrameDraw.Disable();
        }

        private void toolStripButtonFrameOrder_CheckedChanged(object sender, EventArgs e)
        {
            if (toolStripButtonFrameOrder.Checked)
            {
                resetToolStripFrame(toolStripButtonFrameOrder);
                pageEditor.FrameOrder.Enable();
                if (frame != null)
                    frame.EndEdit();
                frame = null;
                updateCurrentFrame();
            }
            else
            {
                pageEditor.FrameOrder.Disable();
                if (listBoxFrames.SelectedIndex >= 0)
                {
                    frame = pageInfo.Frames[listBoxFrames.SelectedIndex];
                    updateCurrentFrame();
                }
            }
            pageEditor.UpdatePage();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listBoxFrames.SelectedIndex >= 0)
            {
                int i = listBoxFrames.SelectedIndex;
                pageInfo.Frames[i].Dispose();
                pageInfo.Frames.RemoveAt(i);
                listBoxFrames.Items.RemoveAt(i);
                if (i == listBoxFrames.Items.Count)
                    i--;
                if (i >= 0)
                    listBoxFrames.SelectedIndex = i;
                else
                {
                    frame = null;
                    updateCurrentFrame();
                }
                pageEditor.UpdatePage();
            }
        }

        private void listBoxFrames_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxFrames.SelectedIndex >= 0)
            {
                if (frame != null)
                    frame.EndEdit();
                frame = null;

                Frame newFrame = pageInfo.Frames[listBoxFrames.SelectedIndex];

                frame = newFrame;
                updateCurrentFrame();
            }
        }
    }
}
